package com.sky.controller.users;

import com.sky.dto.*;
import com.sky.entity.Food;
import com.sky.entity.Museum;
import com.sky.entity.Reservation;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.MuseumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/museum")
@Api(tags = "养生馆相关接口")
public class MuseumController {
    @Autowired
    private MuseumService museumService;

    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有养生馆接口")
    public List<Museum> getMuseumList(){
        List<Museum> museumList = museumService.MuseumList();
        System.out.println(museumList);
        return museumList;
    }

    @GetMapping("/getReservation")
    @ApiOperation(value = "查询用户预约接口")
    public List<Reservation> getReservation(ReservationDTO reservationDTO){
        String phone=reservationDTO.getPhone();
        List<Reservation> reservationList = null;
        if(phone!=null&&phone!=""){
            reservationList = museumService.getReservation(phone);
        }
        return reservationList;
    }

    @GetMapping("/getMuseumByName")
    @ApiOperation(value = "根据理疗馆名称查询接口")
    public Museum getMuseumByName(GetMuseumDTO getMuseumDTO){
        String museumName=getMuseumDTO.getMuseumName();
        Museum museum=new Museum();
        if(museumName!=null&&museumName!=""){
            museum=museumService.getMuseumByName(museumName);
        }
        return museum;
    }

    @PostMapping("/addReservation")
    @ApiOperation(value = "添加预约接口")
    public Result addReservation(@RequestBody ReservationDTO reservationDTO){
        String resTime=reservationDTO.getResTime();
        String phone=reservationDTO.getPhone();
        String museumName=reservationDTO.getMuseumName();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(resTime, dateTimeFormatter);
        Reservation reservation=museumService.selectReservation(dateTime,phone);
        if(reservation!=null){
            return Result.error("该时间段已预约");
        } else {
            InsertReservationDTO insertReservationDTO=new InsertReservationDTO();
            insertReservationDTO.setPhone(phone);
            insertReservationDTO.setResTime(dateTime);
            insertReservationDTO.setMuseumName(museumName);
            insertReservationDTO.setFlag(1L);
            museumService.insertReservation(insertReservationDTO);
            return Result.success();
        }
    }

    @PostMapping("/noReservation")
    @ApiOperation(value = "取消预约接口")
    public Result onReservation(@RequestBody ReservationDTO reservationDTO){
        museumService.deleteReservation(reservationDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "养生馆分页查询接口")
    public Result<PageResult> pageSelect(MuseumPageQueryDTO museumPageQueryDTO){
        PageResult pageResult=museumService.pageQuery(museumPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "理疗馆的展示和不展示")
    public Result startOrStop(@PathVariable("status") Long status,Long id){
        museumService.startOrStop(status,id);
        return Result.success();
    }

    @GetMapping("/getOneMuseum/{id}")
    @ApiOperation(value = "根据理疗馆的ID进行查询")
    public Museum getMuseumById(@PathVariable Long id){
        Museum museum=museumService.getMuseumById(id);
        return museum;
    }
    @GetMapping("/getMuseumById/{id}")
    @ApiOperation(value = "根据ID查询理疗馆")
    public Result<Museum> selectById(@PathVariable Long id){
        Museum museum=museumService.selectById(id);
        return Result.success(museum);
    }

    @PostMapping("/addMuseum")
    @ApiOperation(value = "管理端新增理疗馆接口")
    public Result addMuseum(@RequestBody AddMuseumDTO addMuseumDTO){
        museumService.addMuseum(addMuseumDTO);
        return Result.success();
    }
    @PutMapping("/updateMuseum")
    @ApiOperation(value = "修改理疗馆接口")
    public Result<Museum> updateMuseum(@RequestBody UpdateMuseumDTO updateMuseumDTO){
        museumService.updateMuseum(updateMuseumDTO);
        return Result.success();
    }

    @DeleteMapping("/deleteMuseum/{id}")
    @ApiOperation(value = "删除理疗馆接口")
    public Result deleteMuseum(@PathVariable Long id){
        museumService.deleteMuseum(id);
        return Result.success();
    }



}
